package app.fugema.shop.main.order.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import app.fugema.shop.main.order.adapter.ShoppingOrderCancelAdapter;

import com.base.common.model.bean.ADInfo;
import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.DensityUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.utils.ViewUtils;
import com.base.common.view.base.BaseDialogFragment;
import com.base.common.viewmodel.BaseViewModel;
import app.fugema.shop.R;
import app.fugema.shop.databinding.OrderAfterSaleCancelFmBinding;

import java.util.ArrayList;
import java.util.List;

public class OrderAfterSaleCreateCancelDF extends BaseDialogFragment<OrderAfterSaleCancelFmBinding, BaseViewModel>
{

    public static OrderAfterSaleCreateCancelDF getInstance(List<String> erMsgList,String buyerMsg, OnResultInterface onResultInterface)
    {
        OrderAfterSaleCreateCancelDF shoppingCartBottomFragment = new OrderAfterSaleCreateCancelDF();
        shoppingCartBottomFragment.setOnResultInterface(onResultInterface);
        Bundle args = new Bundle();
        args.putString("buyerMsg", buyerMsg);
        args.putString("msgList", JacksonUtils.transBean2Json(erMsgList));
        shoppingCartBottomFragment.setArguments(args);
        return shoppingCartBottomFragment;
    }

    @Override
    protected OrderAfterSaleCancelFmBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.order_after_sale_cancel_fm, container, false);
    }

    private String buyerMsg;
    private List<String> msgList;

    private OnResultInterface onResultInterface;
    private ShoppingOrderCancelAdapter cancelAdapter;
    private String[] strs = {"7天无理由退换货","商品缺件","商品破损","商品与商家描述不符","商品发错了","商品质量存在问题"};

    public void setOnResultInterface(OnResultInterface onResultInterface) {
        this.onResultInterface = onResultInterface;
    }

    @Override
    public void initView()
    {
        super.initView();
        buyerMsg = getArguments().getString("buyerMsg");

        String listStr = getArguments().getString("msgList");

        msgList = JacksonUtils.getConverList(listStr,String.class);

        binding.ivBack.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                dismissAllowingStateLoss();
            }
        });


        binding.rtvOK.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                ADInfo adInfo = cancelAdapter.getSelectBean();
                if (adInfo != null) {
                    if (onResultInterface != null) {
                        onResultInterface.onSuccess(adInfo.getImageName());
                    }
                }
                dismissAllowingStateLoss();
            }
        });
        cancelAdapter = new ShoppingOrderCancelAdapter();
        cancelAdapter.setRadio(true);
        binding.rvRecyclerView.setAdapter(cancelAdapter);


        List<ADInfo> list = new ArrayList<>();
        if( UIUtils.isNotEmpty( msgList ) )
        {
            for (String str : msgList)
            {
                ADInfo adInfo = new ADInfo();
                adInfo.setImageName(str);
                list.add(adInfo);
            }
        }
        else
        {
            for (String str : strs)
            {
                ADInfo adInfo = new ADInfo();
                adInfo.setImageName(str);
                list.add(adInfo);
            }
        }

        if (UIUtils.isNotEmpty(buyerMsg))
        {
            for (ADInfo adInfo : list)
            {
                if (adInfo.getImageName().equals(buyerMsg)) {
                    adInfo.setSelected(true);
                } else {
                    adInfo.setSelected(false);
                }
            }
        } else {
            list.get(0).setSelected(true);
        }

        cancelAdapter.setDataList(list);

        ViewUtils.setViewBottom(binding.rtvOK, DensityUtil.getDimens(R.dimen.dp_30));
    }


    interface OnResultInterface {
        void onSuccess(String buyerMsg);
    }
}
